package com.isunland.managesystem.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.isunland.managesystem.R;
import com.isunland.managesystem.common.ApiConst;
import com.isunland.managesystem.common.RequestManager;
import com.isunland.managesystem.entity.Task;
import com.isunland.managesystem.utils.LogUtil;

public class FlowChatFragment extends Fragment {

  private static final String TAG = "FlowChatFragment";
  private static final String EXTRA_TASK =
      "com.isunland.managesystem.ui.FlowChatFragment.EXTRA_TASK";
  //	private String mTaskId;
  private Task mTask;
  private String urlImage;
  //	private boolean isntTodo;

  @Override

  public View onCreateView(LayoutInflater inflater, ViewGroup container,
      Bundle savedInstanceState) {
    View v = inflater.inflate(R.layout.fragment_flow_chart, container, false);
    ImageView ivFlowChat = (ImageView) v.findViewById(R.id.iv_flowChat);
    ivFlowChat.setOnClickListener(new OnClickListener() {

      @Override public void onClick(View v) {
        Intent i = new Intent(getActivity(), FullScreenActivity.class);
        i.putExtra(ImageFragment.EXTRA_IMAGE_URL, urlImage);
        startActivity(i);
      }
    });
    try {
      ImageLoader imageLoader = RequestManager.getImageLoader();
      ImageListener listener = ImageLoader.getImageListener(ivFlowChat, R.drawable.progress_round,
          R.drawable.fail_image);

      urlImage = ApiConst.getFlowChatUrl(mTask.isToDo(), mTask.getMyTaksId());
      // /清空缓存,否则流程图不更新
      RequestManager.getRequestQueue(getActivity()).getCache().remove(urlImage);
      imageLoader.get(urlImage, listener);
    } catch (Exception e) {
      e.printStackTrace();
    }

    LogUtil.i(TAG, "url=" + urlImage);
    return v;
  }

  public static FlowChatFragment newInstance(Task task) {
    Bundle args = new Bundle();
    args.putSerializable(EXTRA_TASK, task);
    FlowChatFragment fragment = new FlowChatFragment();
    fragment.setArguments(args);
    return fragment;
  }

  @Override public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mTask = (Task) getArguments().getSerializable(EXTRA_TASK);
    setHasOptionsMenu(true);
    //		mTaskId=getArguments().getString(TaskPagerActivity.EXTRA_TASK_ID);
    //		isntTodo=SharedPreferencesUtil.getBoolean(getActivity(), SharedPreferencesUtil.IS_NOT_TO_DO, false);
  }

  @Override public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == android.R.id.home) {
      getActivity().finish();
    }
    return super.onOptionsItemSelected(item);
  }
}
